<?php

namespace BoxAdmin\Controller;

use Think\Controller;

class OrderController extends CommonController
{
    public function _initialize()
    {
        parent::_initialize();
        $this->memuNav = "orderIndex";
        $this->id = I('id', 0, 'intval');
    }

    /**
     * [index 订单首页]
     * @return [type] [description]
     */
    public function index()
    {
        parent::_initialize();
        $this->memuNav = I("status") ? "order" . I("status") : "orderIndex";
        $kw = I('get.kw', '', 'trim');
        $status = I('get.status');
        $mintime = I('get.mintime');
        $maxtime = I('get.maxtime');
        $minmoney = I('get.minmoney');
        $maxmoney = I('get.maxmoney');
        $unset = I('get.unset');
        if ($minmoney == 0) $minmoney = '0.0';
        $where = $this->isSuper ? "1 " : "ps_order.store_id='$this->store_id'";
        if (!empty($kw)) {
            $where = 'ps_order.order_sn like "%' . $kw . '%" or ps_order.user_id like "%' . $kw . '%" or  ps_order.address like "%' . $kw . '%" or C.store_name like "%' . $kw . '%" or B.payment_name like "%' . $kw . '%"';
        }
        if (!empty($status)) {
            $where .= ' and ps_order.status=' . $status;
        }
        if (!empty($mintime) && !empty($maxtime)) {
            $where .= ' and "' . strtotime($mintime) . '" <= ps_order.add_time && "' . strtotime($maxtime) . '" >=  ps_order.add_time';
        }
        if (!empty($minmoney) && !empty($maxmoney)) {
            $where .= " and (ps_order.order_amount >= '$minmoney' and ps_order.order_amount <= '$maxmoney')";
        }
        if ($unset == '取消查询') {
            $where = $this->isSuper ? "1 " : "ps_order.store_id='$this->store_id'";
        }

        $order = 'ps_order.order_id desc';
        $this->setPager($this->model, 8, $where);
        $data = $this->model->findAll($where, $this->limit, $order);
        foreach ($data as $key => $value) {
            $user = D('ailvoUser')->where(array('user_id' => $value['user_id']))->find();
            $data[$key]['user_name'] = $user['user_name'];
        }
        $this->data = $data;
        $this->display();
    }

    /**
     * [oList 订单详情页]
     * @return [type] [description]
     */
    public function oList()
    {
        $where = "ps_order.order_id='$this->id'";
        $this->data = $this->model->getOne($where);
        $order_id = $this->data['order_id'];
        $this->store_id = $this->data['store_id'];
        $this->vo = $vo = BoxModel("order_logistics")->where("order_id='$order_id'")->join("ps_give G on G.code=ps_order_logistics.give_code")->find();
        // dump($this->vo);
        /*********物流信息***********/
        $a = new \Common\Org\Express();
        $this->result = D('Order')->getWuliu($vo['give_code'], $vo['number']);
        //P($this->result);
        $this->display();
    }

    /**
     * [delete 删除订单]
     * @return [type] [description]
     */
    public function delete()
    {
        if ($this->model->where(array('order_id' => $this->id, 'store_id' => $this->store_id))->delete()) {
            D('Log')->addLogs(session('user_name'), '删除订单');
            $this->redirect('index');
        }
    }

    /**
     * [update 修改订单行为]
     * @return [type] [description]
     */
    public function update()
    {
        if (IS_POST) {
            $data = array(
                'address' => I('post.address'),
                'status' => I('post.status', 0, 'intval'),
                'order_amount' => I('post.order_amount'),
            );
            //dump(I());exit();
            $edit = $this->model->where(array('order_id' => $this->id, 'store_id' => $this->store_id))->save($data);
            D('Log')->addLogs(session('user_name'), '修改订单');
            $array = array(1 => '待付款', 2 => '待发货', 3 => '已发货', 4 => '已完成', 5 => '已取消',);
            $state = I('post.state', '', 'intval');
            $status = I('post.status', '', 'intval');
            //dump($status);exit();
            /* 不等于说明改变状态  */
            if ($status != $state) {
                $res = array(
                    'order_id' => $this->id,
                    'operator' => session('user_name'),
                    'order_status' => $array[$state],
                    'changed_status' => $array[$status],
                    'remark' => '更新状态',
                    'log_time' => time(),
                );
                BoxModel('Order_log')->add($res);
            }
            $this->redirect("oList", array('id' => $this->id, 'store_id' => $this->store_id));

        }
    }

   
  //  public function sendout()
//    {
//        $this->order_id = I('order_id');
//        $this->user_id = I('user_id');
//        $this->wuliu = $this->give->where(array('status' => 1))->select();
//        $this->data = $this->order_logistics->where(array('order_id' => $this->order_id))->find();
//        $this->display();
//    }

    /**
     * 发货
     */
    public function logistics()
    {
					   $this->wuliu = BoxModel("system_logistics")->get(array('status' => 1));
								$this->order_logistics=BoxModel("system_order_logistics");
								$this->goods_ids=I('goods_ids');
								$this->order_sn=I('order_sn');
								$order_info=BoxModel("system_order")->load($this->order_sn);
								$this->data= $this->order_logistics->where(array('order_sn' => $this->order_sn,'goods_ids'=>$this->goods_ids))->find();
								$admin_user_info=session('admin_user_info');
							//	dump(session('admin_user_info'));
						
        if (IS_POST) {
            $data = array(
                'goods_ids' => $this->goods_ids,
                'order_sn' => $this->order_sn,
                'logistics_num' => I('logistics_num'),
                'logistics_code' => I('logistics_code'),
                'remark' => I('remark'),
                'addtime' => time(),
                'remainDate' => I('remainDate'),
                'store_id' => I('store_id'),
																'admin_id'=>session('admin_id'),
																'admin_name'=>session('admin_user_name'),
																'logistics_name'=>I('logistics_name'),
																'user_id' => $order_info['user_id'],
            );
            if ($this->data) {
													   $id=$this->data['id'];
                $r=$this->order_logistics->where(array('id' => $id))->save($data);
            } else {
                 $id=$this->order_logistics->add($data);	
            }
												if($r){
																 BoxModel("system_order")->logistics($this->order_sn,$id);
												}
            $this->jump("操作成功");
        }
								$this->display();
    }

    /****取消一个订单***/
    function cancelOrder()
    {
        $order_id = I("order_id", "", "intval");
        $r = BoxModel("order")->where("order_id='$order_id' and store_id='$this->store_id'")->field("order_id")->find();
        if ($r) {
            BoxModel("order")->cancelOrder($order_id);
        }
        $this->redirect(U("oList", array('id' => $order_id)));
    }

    /*************未签收提醒列表*********/
    function remain()
    {
        $this->memuNav = "remain";
        $model = BoxModel("order_logistics");
        $where = "is_remain=1 and state=0 and ";
        $where .= $this->isSuper ? "1 " : "  O.store_id='$this->store_id'";
        $mintime = I('get.mintime');
        $maxtime = I('get.maxtime');
        if (!empty($mintime) && !empty($maxtime)) {
            $where .= ' and "' . strtotime($mintime) . '" <= addtime && "' . strtotime($maxtime) . '" >=  addtime';
        }
        if ($order_id = I('order_id')) {
            $where .= " and O.order_sn='$order_id'";
        }
        if ($this->remainDate = I('remainDate')) {
            $where .= " and remainDate='$this->remainDate'";
        }
        $this->setPager($model, 8, $where);
        $this->data = $model->join("ps_order O on O.order_id=ps_order_logistics.order_id")->limit($this->limit)->where($where)->select();
        //dump($this->data);
        $this->display();
    }

    /*******订单标识为已经签收********/
    function sign_order()
    {
        $order_id = I('order_id');
        $model = BoxModel("order_logistics");
        $model->where("order_id='$order_id'")->save(array('state' => $state));
        $this->jump("操作成功");
    }

    /**
     * 退货记录
     */
    public function orderReturn()
    {
        $this->memuNav = "orderReturn";
        $where = $this->isSuper ? "1 " : "ps_order_return.store_id='$this->store_id'";
        $order = 'ps_order_return.id desc';
        $model = BoxModel('order_return');
        $this->setPager($model, 8, $where);
        $this->data = $model
            //->join('left join ps_order_goods O on O.order_id=ps_order_return.order_id')
            ->join('left join ps_order R on R.order_id=ps_order_return.order_id')
            ->where($where)
            ->field('ps_order_return.*,R.order_sn')
            ->order($order)
            ->limit($this->limit)
            ->select();
        $this->display();
    }

    /**
     * 详细
     */
    public function returnList()
    {
        $this->memuNav = "orderReturn";
        $id = I('id');
        $data = BoxModel('order_return')->where(array('store_id' => $this->store_id, 'id' => $id))->find();
        $order_goods = BoxModel('order_goods')->where(array('order_id' => $data['order_id']))->find();
        $member = BoxModel('member_auth')->where(array('user_id' => $data['user_id']))->find();
        $order = BoxModel('order')->where(array('order_id' => $data['order_id']))->find();
        $data['goods_name'] = $order_goods['goods_name'];
        $data['nickname'] = $member['nickname'];
        $data['order_sn'] = $order['order_sn'];
        if ($data['piclist']) {
            $data['images'] = explode(',', $data['piclist']);
        }
        $this->data = $data;
        $this->display();
    }

    /**
     * 删除
     */
    public function return_delete()
    {
        if ($id = I('id')) {
            BoxModel('order_return')->delete($id);
            $this->redirect('Order/orderReturn');
        }
    }

    public function return_edit()
    {
        if (IS_POST) {
            if ($id = I('id')) {
                BoxModel('order_return')->where(array('store_id' => $this->store_id, 'id' => $id))->setField('state', I('state'));
                $this->redirect('Order/orderReturn');
            }
        }
    }


}